package org.zwy.gradetrack.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.zwy.gradetrack.common.Result;
import org.zwy.gradetrack.entity.Teacher;
import org.zwy.gradetrack.service.TeacherService;

import java.util.List;

/**
 * 教师管理控制器
 */
@RestController
@RequestMapping("/api/teachers")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 分页查询教师列表
     */
    @GetMapping("/page")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public ResponseEntity<Result<Page<Teacher>>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String teacherId,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer majorId) {
        Page<Teacher> page = new Page<>(current, size);
        return ResponseEntity.ok(Result.success(teacherService.pageTeachers(current, size, name, teacherId, title, majorId)));
    }

    /**
     * 获取所有教师
     */
    @GetMapping
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public ResponseEntity<Result<List<Teacher>>> list() {
        return ResponseEntity.ok(Result.success(teacherService.getAllTeachers()));
    }

    /**
     * 创建教师
     */
    @PostMapping
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> create(@RequestBody Teacher teacher) {
        try {
            return ResponseEntity.ok(Result.success(teacherService.createTeacher(teacher)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }

    /**
     * 更新教师
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> update(@PathVariable Integer id, @RequestBody Teacher teacher) {
        try {
            teacher.setId(id);
            return ResponseEntity.ok(Result.success(teacherService.updateTeacher(teacher)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }

    /**
     * 删除教师
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> delete(@PathVariable Integer id) {
        try {
            return ResponseEntity.ok(Result.success(teacherService.deleteTeacher(id)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }

    /**
     * 获取教师详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public ResponseEntity<Result<Teacher>> getById(@PathVariable Integer id) {
        try {
            return ResponseEntity.ok(Result.success(teacherService.getTeacherById(id)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(404, e.getMessage()));
        }
    }

    /**
     * 根据姓名查询教师
     */
    @GetMapping("/search/name")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public ResponseEntity<Result<List<Teacher>>> getByName(@RequestParam String name) {
        return ResponseEntity.ok(Result.success(teacherService.getTeachersByName(name)));
    }

    /**
     * 批量导入教师
     */
    @PostMapping("/batch")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> batchImport(@RequestBody List<Teacher> teachers) {
        try {
            return ResponseEntity.ok(Result.success(teacherService.batchImport(teachers)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }
} 